/**
  ******************************************************************************
  * @file    Libraries/Device/TS32Fx/TS32Fx_LL_Driver/src/ts32fx_ll_adkey.c
  * @author  TOPSYS Application Team
  * @version V1.0.0
  * @date    02-11-2018
  * @brief   This file contains all the ADKEY LL firmware functions.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2018 TOPSYS</center></h2>
  *
  *
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "include.h"

/** @addtogroup TS32Fx_StdPeriph_Driver TS32Fx Driver
  * @{
  */
  
/** @defgroup adkey_interface_gr ADKEY Driver
  * @ingroup  TS32Fx_StdPeriph_Driver
  * @{
  */

/**
  * @brief  Low layer adc once mode init function
  * @param  p_adc: ponit of adkey
  * @param  p_init : Configure the adc initialization structure
  * @retval None
  */
void ll_adc_once_sample_init(ADKEY_TypeDef *p_adc, TYPE_LL_ADC_ONCE_SAMPLE_INIT *p_init)
{
    u32 adc_temp = 0;
    adc_temp = p_adc->CR;
    adc_temp = (adc_temp & (~LL_ADKEY_CR_ADC_MD(ALL_WORD_FF))) | LL_ADKEY_CR_ADC_MD(LL_ADC_MODE_ONCE);
    adc_temp = (adc_temp & (~LL_ADKEY_CR_ADC_ALIGN(ALL_WORD_FF))) | LL_ADKEY_CR_ADC_ALIGN(p_init->data_align);
    p_adc->CR = adc_temp;
    
    adc_temp = p_adc->STA;
    adc_temp = (adc_temp & (~LL_ADKEY_STA_ADC_DONE_PENDING) & (~LL_ADKEY_STA_ADC_ONCE_MD_CH_SEL(ALL_WORD_FF))) | LL_ADKEY_STA_ADC_ONCE_MD_CH_SEL(p_init->adc_channel);
    p_adc->STA = adc_temp;
    
    adc_temp = p_adc->CFG;
    adc_temp = (adc_temp & (~LL_ADKEY_CFG_ADC_PSC_MASK)) | LL_ADKEY_CFG_ADC_PSC(p_init->adc_psc);
    if(p_init->adc_en)
    {
        adc_temp |= LL_ADKEY_CFG_ADC_EN;
    }
    else
    {
        adc_temp &= ~LL_ADKEY_CFG_ADC_EN;
    }
    p_adc->CFG = adc_temp;
}

/**
  * @brief  Low layer adc single cycle mode init function
  * @param  p_adc: ponit of adkey
  * @param  p_init : Configure the adc initialization structure
  * @retval None
  */
void ll_adc_single_cycle_init(ADKEY_TypeDef *p_adc, TYPE_LL_ADC_SINGLE_CYCLE_INIT *p_init)
{
    u32 adc_temp = 0;
    adc_temp = p_adc->CR;
    adc_temp = (adc_temp & (~LL_ADKEY_CR_ADC_MD(ALL_WORD_FF))) | LL_ADKEY_CR_ADC_MD(LL_ADC_MODE_SINGLE_CYCLE);
    adc_temp = (adc_temp & (~LL_ADKEY_CR_ADC_ALIGN(ALL_WORD_FF))) | LL_ADKEY_CR_ADC_ALIGN(p_init->data_align);
    adc_temp = (adc_temp & (~LL_ADKEY_CR_ADC_SCAN_DIR(ALL_WORD_FF))) | LL_ADKEY_CR_ADC_SCAN_DIR(p_init->adc_scan_direction);
    if(p_init->adc_dma_en)
    {
        adc_temp |= LL_ADKEY_CR_ADC_DMA_EN;
        ADKEY->DMAADR = p_init->adc_dma_addr;
    }
    else
    {
        adc_temp &= ~LL_ADKEY_CR_ADC_DMA_EN;
    }
    p_adc->CR = adc_temp;
    
    ADKEY->CHS = p_init->adc_channel_map;
    
    adc_temp = p_adc->CFG;
    adc_temp = (adc_temp & (~LL_ADKEY_CFG_ADC_PSC_MASK)) | LL_ADKEY_CFG_ADC_PSC(p_init->adc_psc);
    adc_temp = (adc_temp & (~LL_ADKEY_CFG_ADC_D2DCYC(ALL_WORD_FF))) | LL_ADKEY_CFG_ADC_D2DCYC(p_init->d2dcyc);
    if(p_init->adc_en)
    {
        adc_temp |= LL_ADKEY_CFG_ADC_EN;
    }
    else
    {
        adc_temp &= ~LL_ADKEY_CFG_ADC_EN;
    }
    p_adc->CFG = adc_temp;
}

/**
  * @brief  Low layer adc continuous mode init function
  * @param  p_adc: ponit of adkey
  * @param  p_init : Configure the adc initialization structure
  * @retval None
  */
void ll_adc_continuous_cycle_init(ADKEY_TypeDef *p_adc, TYPE_LL_ADC_CONTINUOUS_CYCLE_INIT *p_init)
{
    u32 adc_temp = 0;
    adc_temp = p_adc->CR;
    adc_temp = (adc_temp & (~LL_ADKEY_CR_ADC_MD(ALL_WORD_FF))) | LL_ADKEY_CR_ADC_MD(LL_ADC_MODE_CONTINUOUS);
    adc_temp = (adc_temp & (~LL_ADKEY_CR_ADC_ALIGN(ALL_WORD_FF))) | LL_ADKEY_CR_ADC_ALIGN(p_init->data_align);
    adc_temp = (adc_temp & (~LL_ADKEY_CR_ADC_SCAN_DIR(ALL_WORD_FF))) | LL_ADKEY_CR_ADC_SCAN_DIR(p_init->adc_scan_direction);
    if(p_init->adc_dma_en)
    {
        adc_temp |= LL_ADKEY_CR_ADC_DMA_EN;
        ADKEY->DMAADR = p_init->adc_dma_addr;
    }
    else
    {
        adc_temp &= ~LL_ADKEY_CR_ADC_DMA_EN;
    }
    p_adc->CR = adc_temp;
    
    ADKEY->CHS = p_init->adc_channel_map;
    
    adc_temp = p_adc->CFG;
    adc_temp = (adc_temp & (~LL_ADKEY_CFG_ADC_PSC_MASK)) | LL_ADKEY_CFG_ADC_PSC(p_init->adc_psc);
    adc_temp = (adc_temp & (~LL_ADKEY_CFG_ADC_D2DCYC(ALL_WORD_FF))) | LL_ADKEY_CFG_ADC_D2DCYC(p_init->d2dcyc);
    if(p_init->adc_en)
    {
        adc_temp |= LL_ADKEY_CFG_ADC_EN;
    }
    else
    {
        adc_temp &= ~LL_ADKEY_CFG_ADC_EN;
    }
    p_adc->CFG = adc_temp;
}

/** @addtogroup ADKEY_LL_Driver ADKEY LL Driver
  * @ingroup  adkey_interface_gr
  * @{
  */
  
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

  
/** @defgroup ADKEY_LL_Interrupt ADKEY LL Interrupt Handle function
  * @ingroup  ADKEY_LL_Driver
  * @brief   ADKEY LL Interrupt Handle function
  * @{
  */



/**
  * @}
  */

/** @defgroup ADKEY_LL_Inti_Cfg ADKEY LL Initialization And Configuration
  * @ingroup  ADKEY_LL_Driver
  * @brief    ADKEY LL Initialization And Configuration
  * @{
  */


/**
  * @}
  */

/** @defgroup ADKEY_LL_Data_Transfers ADKEY LL Data transfers functions
  * @ingroup  ADKEY_LL_Driver
  * @brief    ADKEY LL Data transfers functions 
  * @{
  */

/**
  * @brief  Low layer adc interrupt enable function
  * @param  None
  * @retval None
  */
void ll_adc_interrupt_enable(void)
{
    ADKEY->CR |= LL_ADKEY_CR_ADC_IE;
}

/**
  * @brief  Low layer adc interrupt disable function
  * @param  None
  * @retval None
  */
void ll_adc_interrupt_disable(void)
{
    ADKEY->CR &= ~LL_ADKEY_CR_ADC_IE;
}

/**
  * @brief  Low layer adc over run interrupt enable function
  * @param  None
  * @retval None
  */
void ll_adc_ovr_interrupt_enable(void)
{
    ADKEY->CR |= LL_ADKEY_CR_ADC_OVR_IE;
}

/**
  * @brief  Low layer adc over run interrupt disable function
  * @param  None
  * @retval None
  */
void ll_adc_ovr_interrupt_disable(void)
{
    ADKEY->CR &= ~LL_ADKEY_CR_ADC_OVR_IE;
}

/**
  * @brief  Low layer adc enable function
  * @param  None
  * @retval None
  */
void ll_adc_enable(void)
{
    ADKEY->CFG |= LL_ADKEY_CFG_ADC_EN;
}

/**
  * @brief  Low layer adc disable function
  * @param  None
  * @retval None
  */
void ll_adc_disable(void)
{
    ADKEY->CFG &= ~LL_ADKEY_CFG_ADC_EN;
}

/**
  * @brief  Low layer adc dma enable function
  * @param  None
  * @retval None
  */
void ll_adc_dma_enable(void)
{
    ADKEY->CR |= LL_ADKEY_CR_ADC_DMA_EN;
}

/**
  * @brief  Low layer adc dma disable function
  * @param  None
  * @retval None
  */
void ll_adc_dma_disable(void)
{
    ADKEY->CR &= ~LL_ADKEY_CR_ADC_DMA_EN;
}

/**
  * @brief  Low layer adc external interrupt source enable function
  * @param  None
  * @retval None
  */
void ll_adc_ext_trg_src_enable(void)
{
    ADKEY->CR |= LL_ADKEY_CR_ADC_EXT_TRG_EN;
}

/**
  * @brief  Low layer adc external interrupt source disable function
  * @param  None
  * @retval None
  */
void ll_adc_ext_trg_src_disable(void)
{
    ADKEY->CR &= ~LL_ADKEY_CR_ADC_EXT_TRG_EN;
}

/**
  * @brief  Low layer adc dma enable function
  * @param  None
  * @retval None
  */
void ll_adc_start_enable(void)
{
    ADKEY->CR |= LL_ADKEY_CR_ADC_ST_EN;
}

/**
  * @brief  Low layer adc dma disable function
  * @param  None
  * @retval None
  */
void ll_adc_start_disable(void)
{
    ADKEY->CR &= ~LL_ADKEY_CR_ADC_ST_EN;
}


/**
  * @brief  Low layer dac0 set value function
  * @param  None
  * @retval None
  */
void ll_dac0_set_data(u8 data)
{
    DAC->CON = (DAC->CON & (~LL_DAC_CON_DAT(ALL_WORD_FF))) | LL_DAC_CON_DAT(data);
}

/**
  * @brief  Low layer dac0 enable function
  * @param  None
  * @retval None
  */
void ll_dac0_enable(void)
{

    DAC->CON |= LL_DAC_CON_EN;
}

/**
  * @brief  Low layer dac0 disable function
  * @param  None
  * @retval None
  */
void ll_dac0_disable(void)
{

    DAC->CON &= ~LL_DAC_CON_EN;
}

/**
  * @brief  Low layer adc1 set value function
  * @param  None
  * @retval None
  */
void ll_dac1_set_data(u16 data)
{
    ADKEY->DADATA = LL_ADKEY_DADAT_DAC_DAT(data);
    
}

/**
  * @brief  Low layer adc1 enable function
  * @param  None
  * @retval None
  */
void ll_dac1_enable(void)
{
    ADKEY->CFG |= LL_ADKEY_CFG_DAC_EN;
}

/**
  * @brief  Low layer adc1 disable function
  * @param  None
  * @retval None
  */
void ll_dac1_disable(void)
{
    ADKEY->CFG &= ~LL_ADKEY_CFG_DAC_EN;
}





/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/*************************** (C) COPYRIGHT 2018 TOPSYS ***** END OF FILE *****/
